package com.xauto.pioneer.appuser.view;

import android.app.Dialog;
import android.content.Context;
import android.text.SpannableStringBuilder;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xauto.pioneer.appuser.R;
import com.xauto.pioneer.appuser.util.StringUtils;

/**
 * Created by Lemon on 2014/7/5.
 * Desc:自定义提示对话框
 */
public class PromptDialog extends Dialog {

    private LinearLayout title_bar;
    private TextView tvDialogTitle;
    private WebView tvDialogWebView;
    private TextView tvDialogMsg;
    private TextView tvDialogConfirm;
    private TextView tvDialogCancel;
    private String title;
    private CharSequence msg;
    private String confirmText;
    private String cancelText;
    private ImageView img_notice;
    private String imgType;
    private View.OnClickListener OnConfirmListener;
    private View.OnClickListener OnCancelListener;
    private boolean cancelBtnClick = true;
    private boolean isClickOutside = false;
    private boolean isWebView = false;

    /***
     * 普通的错误提示框：只有一个确定按钮，并且只是提示，不做事件处理
     * @param context 上下文
     * @param msg 错误内容
     */
    public PromptDialog(Context context, String msg) {
        super(context, R.style.LoadingDialogTheme);
        this.msg = msg;
        this.title = context.getString(R.string.notice);
        this.confirmText = context.getString(R.string.confirm);
        this.OnConfirmListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        };
        init(context);
    }

    /***
     * 只有一个确定按钮的提示对话框
     * @param context 上下文
     * @param title 标题
     * @param msg 内容
//     * @param img 提示图片;
     * @param confirmText 确认按钮文字
     * @param OnConfirmListener 确认事件监听
     */
    public PromptDialog(Context context, String title, String msg, String confirmText,String imgType,
                        View.OnClickListener OnConfirmListener) {
        super(context, R.style.LoadingDialogTheme);
        this.title = title;
        this.msg = msg;
        this.confirmText = confirmText;
        this.imgType = imgType;
        this.OnConfirmListener = OnConfirmListener;
        init(context);
    }

    /***
     * 提示对话框
     * @param context 上下文
     * @param title 标题
     * @param msg 内容
     * @param confirmText 确认按钮文字
     * @param cancelText 取消按钮文字
     * @param OnConfirmListener 确定事件监听
     * @param OnCancelListener 取消事件监听
     */
    public PromptDialog(Context context, String title, String msg, String confirmText,
                        String cancelText, String imgType,View.OnClickListener OnConfirmListener,
                        View.OnClickListener OnCancelListener) {
        super(context, R.style.LoadingDialogTheme);
        this.title = title;
        this.msg = msg;
        this.imgType = imgType;
        this.confirmText = confirmText;
        this.cancelText = cancelText;
        this.OnConfirmListener = OnConfirmListener;
        this.OnCancelListener = OnCancelListener;
        init(context);
    }
    /***
     * 提示对话框
     * @param context 上下文
     * @param title 标题
     * @param msg 内容
     * @param confirmText 确认按钮文字
     * @param cancelText 取消按钮文字
     * @param OnConfirmListener 确定事件监听
     * @param OnCancelListener 取消事件监听
     * @param cancelBtnClick 取消事件是否可以点击
     */
    public PromptDialog(Context context, String title, String msg, String confirmText,
                        String cancelText,String imgType, View.OnClickListener OnConfirmListener,
                        View.OnClickListener OnCancelListener,boolean cancelBtnClick) {
        super(context, R.style.LoadingDialogTheme);
        this.title = title;
        this.msg = msg;
        this.imgType = imgType;
        this.confirmText = confirmText;
        this.cancelText = cancelText;
        this.OnConfirmListener = OnConfirmListener;
        this.OnCancelListener = OnCancelListener;
        this.cancelBtnClick = cancelBtnClick;
        init(context);
    }
    /***
     * 提示对话框
     * @param context 上下文
     * @param title 标题
     * @param msg 内容
     * @param confirmText 确认按钮文字
     * @param cancelText 取消按钮文字
     * @param OnConfirmListener 确定事件监听
     * @param OnCancelListener 取消事件监听
     * @param cancelBtnClick 取消事件是否可以点击
     */
    public PromptDialog(Context context, String title, String msg, String confirmText,
                        String cancelText,String imgType, View.OnClickListener OnConfirmListener,
                        View.OnClickListener OnCancelListener,boolean cancelBtnClick,boolean isClickOutside) {
        super(context, R.style.LoadingDialogTheme);
        this.title = title;
        this.msg = msg;
        this.confirmText = confirmText;
        this.cancelText = cancelText;
        this.imgType = imgType;
        this.OnConfirmListener = OnConfirmListener;
        this.OnCancelListener = OnCancelListener;
        this.cancelBtnClick = cancelBtnClick;
        this.isClickOutside = isClickOutside;
        init(context);
    }
    /***
     * 提示对话框
     * @param context 上下文
     * @param title 标题
     * @param msg 内容
     * @param confirmText 确认按钮文字
     * @param cancelText 取消按钮文字
     * @param OnConfirmListener 确定事件监听
     * @param OnCancelListener 取消事件监听
     */
    public PromptDialog(Context context, String title, SpannableStringBuilder msg, String confirmText,
                        String cancelText, String imgType, View.OnClickListener OnConfirmListener,
                        View.OnClickListener OnCancelListener) {
        super(context, R.style.LoadingDialogTheme);
        this.title = title;
        this.msg = msg;
        this.imgType = imgType;
        this.confirmText = confirmText;
        this.cancelText = cancelText;
        this.OnConfirmListener = OnConfirmListener;
        this.OnCancelListener = OnCancelListener;
        init(context);
    }

    /***
     * 提示对话框
     * @param context 上下文
     * @param title 标题
     * @param msg 内容
     * @param confirmText 确认按钮文字
     * @param cancelText 取消按钮文字
     * @param OnConfirmListener 确定事件监听
     * @param OnCancelListener 取消事件监听
     */
    public PromptDialog(Context context, String title, String msg, String confirmText,
                        String cancelText,String imgType, View.OnClickListener OnConfirmListener,
                        View.OnClickListener OnCancelListener,boolean isWebView,String str) {
        super(context, R.style.LoadingDialogTheme);
        this.title = title;
        this.msg = msg;
        this.confirmText = confirmText;
        this.imgType = imgType;
        this.cancelText = cancelText;
        this.OnConfirmListener = OnConfirmListener;
        this.OnCancelListener = OnCancelListener;
        this.isWebView = isWebView;
        init(context);
    }

    private void init(Context context) {
        setContentView(R.layout.dialog_prompt);

        title_bar = (LinearLayout) findViewById(R.id.title_bar);
        tvDialogTitle = (TextView) findViewById(R.id.tvDialogTitle);
        tvDialogWebView = (WebView) findViewById(R.id.tvDialogWebView);

        img_notice = (ImageView) findViewById(R.id.img_notice);

        if (StringUtils.isEmpty(title)){
            title_bar.setVisibility(View.GONE);
        }else {
            title_bar.setVisibility(View.VISIBLE);
            tvDialogTitle.setText(title);
        }
        tvDialogMsg = (TextView) findViewById(R.id.tvDialogMsg);
        tvDialogConfirm = (TextView) findViewById(R.id.tvDialogConfirm);
        tvDialogConfirm.setText(confirmText);
        tvDialogConfirm.setOnClickListener(OnConfirmListener);
        tvDialogCancel = (TextView) findViewById(R.id.tvDialogCancel);

        if (isWebView){
            tvDialogWebView.loadData(msg.toString(), "text/html; charset=UTF-8", null);
            tvDialogWebView.setVisibility(View.VISIBLE);
            tvDialogMsg.setVisibility(View.GONE);

        }else {
            tvDialogWebView.setVisibility(View.GONE);
            tvDialogMsg.setVisibility(View.VISIBLE);
            img_notice.setVisibility(View.VISIBLE);
            tvDialogMsg.setText(msg);
        }

        switch (imgType){
            case "notice":
                img_notice.setVisibility(View.VISIBLE);
                img_notice.setImageResource(R.mipmap.ic_common_failed);
                break;
            case "nopic":
                img_notice.setVisibility(View.GONE);
                break;
            case "cache":
                img_notice.setVisibility(View.VISIBLE);
                img_notice.setImageResource(R.mipmap.ic_clearcache);
                break;
            case "common":
                img_notice.setVisibility(View.VISIBLE);
                img_notice.setImageResource(R.mipmap.ic_common_notice);
        }
        tvDialogCancel.setClickable(cancelBtnClick ? true:false);
        tvDialogCancel.setTextColor(cancelBtnClick ? context.getResources().getColor(R.color.main_black) : context.getResources().getColor(R.color.light_gray));
        tvDialogCancel.setText(cancelText);
        tvDialogCancel.setOnClickListener(OnCancelListener);
        if (OnCancelListener == null)
            tvDialogCancel.setVisibility(View.GONE);//只显示一个按钮
        if (StringUtils.isBlank(title))
            tvDialogTitle.setVisibility(View.GONE);
        setCanceledOnTouchOutside(isClickOutside);
        setCancelable(isClickOutside);
    }
}
